package co.baiku.boot.common.tools;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

public class SerializeTools {
    private static Logger log = LoggerFactory.getLogger(SerializeTools.class);

    private SerializeTools() {
    }

    public static byte[] serialize(Object o) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            ObjectOutputStream outo = new ObjectOutputStream(out);
            outo.writeObject(o);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }

        return out.toByteArray();
    }

    public static Object deserialize(byte[] b) {
        ObjectInputStream oin;
        try {
            oin = new ObjectInputStream(new ByteArrayInputStream(b));
            try {
                return oin.readObject();
            } catch (ClassNotFoundException e) {
                log.error(e.getMessage(), e);
                return null;
            }
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            return null;
        }

    }
}
